package com.ys.controller.web.finance;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.finance.TcwExpenseAllocationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwExpenseAllocation;
import com.ys.entity.finance.TcwExpenseAllocationItem;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/finance/tcwExpenseAllocation")
public class WebTcwExpenseAllocationController {
	private static final Logger logger = LoggerFactory.getLogger(WebTcwExpenseAllocationController.class);

	@Autowired
	private CommonApiService<TcwExpenseAllocation> commonApiService;

	//菜单id
	private Long menuId = MenuId.TCW_ExpenseAllocation;

	@Autowired
	private YspTaskService yspTaskService;

	@Autowired
	private TcwExpenseAllocationService tcwExpenseAllocationService;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){
	  try {
            Map<String, Object> extraParam = new HashMap<>();
            //关联数据
            extraParam.put("status", 1);

            //是否校验 1是、0否
		  	extraParam.put("isCheck", 1);

		  	TcwExpenseAllocation entity = new TcwExpenseAllocation();
            //关联数据
            String bid = commonApiService.getCommonNextBid(menuId);
            entity.setBid(bid);

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId, new ArrayList<Map<String, Object>>());
            return commonApiService.toAdd(model, entity, menuId, null, subGridConfig);
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


	/*
	 * 新增，根据导入数据
	 */
	@GetMapping("/toAddByImport")
	public String toAddByImport(Model model, HttpServletRequest request, HttpServletResponse response, String bid){
		try {
			//主表信息
			Map<String, Object> extraParam = tcwExpenseAllocationService.getExtraParam(bid);
			//子表信息
			List<Map<String, Object>> itemList = tcwExpenseAllocationService.getItemList(bid);
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId,itemList);

			//是否校验 1是、0否
			extraParam.put("isCheck", 1);

			//子表按钮
			Integer state = yspTaskService.flowState(bid);
			if (state == Flow.State.FINISH.value){
				subGridConfig.setButtonArr(new ArrayList<>());
			}else if (state == Flow.State.PROCESS.value){
				subGridConfig.setButtonArr(new ArrayList<>());
			}

			TcwExpenseAllocation entity = new TcwExpenseAllocation()
					.setBid(bid)
					.setCreateTime(new Date());

			return commonApiService.toAdd(model,entity, menuId, extraParam, subGridConfig);

		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}


	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
		try {
			//主表信息
			Map<String, Object> extraParam = tcwExpenseAllocationService.getExtraParam(bid);
			//子表信息
			List<Map<String, Object>> itemList = tcwExpenseAllocationService.getItemList(bid);
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId,itemList);

//			//是否校验 1是、0否
//			extraParam.put("isCheck", 1);

			//子表按钮
			Integer state = yspTaskService.flowState(bid);
			if (state == Flow.State.FINISH.value){
				subGridConfig.setButtonArr(new ArrayList<>());
			}else if (state == Flow.State.PROCESS.value){
				subGridConfig.setButtonArr(new ArrayList<>());
			}

			return commonApiService.toEdit(model,Long.parseLong(extraParam.get("id").toString()), new TcwExpenseAllocation(), menuId, extraParam, subGridConfig);
		}catch (Exception e) {
			logger.error("编辑页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}





}